package com.lbs.droiddoctor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lbs.droiddoctor.data.DiseaseData;
import com.lbs.droiddoctor.database.DbManager;

/**
 * Servlet implementation class Disease
 */
@WebServlet("/Disease")
public class Disease extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Disease() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method.equals("add")) {
			addDisease(request, response);
		} else if (method.equals("get")) {
			getDisease(request, response);
		} else if (method.equals("search")) {
			searchDisease(request, response);
		} else if (method.equals("delete")) {
			deleteDisease(request, response);
		}
	}

	private void deleteDisease(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter pw = response.getWriter();
		DbManager db = DbManager.getInstance();
		String disease = request.getParameter("disease");
		if (disease != null) {
			int ret = db.deleteDisease(disease);
			if (ret == 1) {
				pw.println("Disease Deleted ");
			} else {
				pw.println("Disease was not able to delete");
			}
		}

	}

	private void searchDisease(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter pw = response.getWriter();
		DbManager db = DbManager.getInstance();
		String key = request.getParameter("key");
		List<DiseaseData> dis = db.getdisease(key);
		// convert java object to JSON format,
		// and returned as JSON formatted string
		Gson gson = new Gson();
		String json = gson.toJson(dis);
		pw.println(json);

	}

	private void getDisease(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter pw = response.getWriter();
		DbManager db = DbManager.getInstance();

		List<DiseaseData> dis = db.getdisease();
		// convert java object to JSON format,
		// and returned as JSON formatted string
		Gson gson = new Gson();
		String json = gson.toJson(dis);
		pw.println(json);

	}

	private void addDisease(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String symptoms = request.getParameter("sym");
		String disease = request.getParameter("disease");
		String[] symptomArray = null;
		if (symptoms != null) {
			symptomArray = symptoms.split(",");
		}
		DbManager db = DbManager.getInstance();
		PrintWriter pw = response.getWriter();
		/*if (db.getDiseaseId(disease) != -1) {
			pw.println("Disease already exists...");
		} else {*/
			if (disease!=null && symptomArray != null) {
				db.addDisease(disease, symptomArray);
				pw.println("Disease added successfully...");
			} else {
				pw.println("No Symptoms given to enter...");
			}
		//}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
